package com.ns.basic.server.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ns.basic.server.dto.ServerPermissionDto;
import com.ns.basic.server.entry.ServerMenuEntry;
import com.ns.basic.server.mapper.ServerMenuEntryMapper;
import com.ns.core.current.config.SysConst;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 服务模块业务实现接口
 *
 * @author lw
 */
@Service
@RequiredArgsConstructor
public class ServerMenuServiceImpl extends ServiceImpl<ServerMenuEntryMapper, ServerMenuEntry> implements IServerMenuService {

    private final ServerMenuEntryMapper mapper;

    @Override
    public boolean assistMenu(ServerPermissionDto dto) {
        boolean flag = Boolean.FALSE;
        if (!CollectionUtils.isEmpty(dto.getPermissions())) {
            List<ServerMenuEntry> sms = new ArrayList<>(dto.getPermissions().size());
            for (List<Long> permission : dto.getPermissions()) {
                if (!CollectionUtils.isEmpty(permission)) {
                    ServerMenuEntry sm = new ServerMenuEntry();
                    sm.setSid(dto.getId());
                    switch (permission.size()) {
                        case 1:
                            sm.setOne(permission.get(0));
                            break;
                        case 2:
                            sm.setOne(permission.get(0));
                            sm.setTwo(permission.get(1));
                            break;
                        case 3:
                            sm.setOne(permission.get(0));
                            sm.setTwo(permission.get(1));
                            sm.setThree(permission.get(2));
                            break;
                        case 4:
                            sm.setOne(permission.get(0));
                            sm.setTwo(permission.get(1));
                            sm.setThree(permission.get(2));
                            sm.setFour(permission.get(3));
                            break;
                        default:
                            throw new IllegalStateException("权限超出范围: " + permission.size());
                    }
                    sms.add(sm);
                }
            }
            mapper.deleteBySid(dto.getId());
            mapper.insert(sms);
            flag = Boolean.TRUE;
        }
        return flag;
    }

    @Override
    @Cacheable(cacheNames = SysConst.SERVER_PERMISSION, key = "#id", unless = "#result==null")
    public List<List<String>> getPermissions(Long id) {
        List<List<String>> permissions = new ArrayList<>();
        List<ServerMenuEntry> serverMenus = mapper.selectBySid(id);
        if (!CollectionUtils.isEmpty(serverMenus)) {
            for (ServerMenuEntry serverMenu : serverMenus) {
                List<String> permission = new ArrayList<>();
                permission.add(String.valueOf(serverMenu.getOne()));
                if (Objects.nonNull(serverMenu.getTwo())) {
                    permission.add(String.valueOf(serverMenu.getTwo()));
                }
                if (Objects.nonNull(serverMenu.getThree())) {
                    permission.add(String.valueOf(serverMenu.getThree()));
                }
                if (Objects.nonNull(serverMenu.getFour())) {
                    permission.add(String.valueOf(serverMenu.getFour()));
                }
                permissions.add(permission);
            }
        }
        return permissions;
    }

    @Override
    public List<Long> getServerMenus(Long id) {
        List<Long> list = new ArrayList<>();
        List<ServerMenuEntry> serverMenus = mapper.selectBySid(id);
        if (!CollectionUtils.isEmpty(serverMenus)) {
            Set<Long> serverMenuList = new HashSet<>();
            for (ServerMenuEntry serverMenu : serverMenus) {
                serverMenuList.add(serverMenu.getOne());
                if (Objects.nonNull(serverMenu.getTwo())) {
                    serverMenuList.add(serverMenu.getTwo());
                }
                if (Objects.nonNull(serverMenu.getThree())) {
                    serverMenuList.add(serverMenu.getThree());
                }
                if (Objects.nonNull(serverMenu.getFour())) {
                    serverMenuList.add(serverMenu.getFour());
                }
            }
            list.addAll(serverMenuList);
        }
        return list;
    }
}
